﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Scheduler;

namespace Temporizador
{
    public partial class Page1 : PhoneApplicationPage
    {
        private void AddReminderOrAlarm(object sender, EventArgs e)
        {
            string name = System.Guid.NewGuid().ToString();
            DateTime beginTime = GetDate(beginDatePicker, beginTimePicker);
            ValidateDate(beginTime, DateTime.Now, "The begin date must be in the future. ");
            DateTime expirationTime = GetDate(expirationDatePicker, expirationTimePicker);
            ValidateDate(expirationTime, beginTime, "The expiration date should be greater than begin date. ");
            RecurrenceInterval recurrence = GetRecurrenceInterval();
            Uri navigationUri = GetUrl();
            if (reminderRadioButton.IsChecked.Value)
            {
                CreateReminder(name, beginTime, expirationTime, recurrence, navigationUri);
            }
            else
            {
                CreateAlarm(name, beginTime, expirationTime, recurrence);
            }
            NavigationService.GoBack();
        }

        private void CreateAlarm(string name, DateTime beginTime, DateTime expirationTime, RecurrenceInterval recurrence)
        {
            Alarm alarm = new Alarm(name);
            alarm.Content = contentTextBox.Text;
            alarm.Sound = new Uri("/Ringtones/Ring01.wma", UriKind.Relative);
            alarm.BeginTime = beginTime;
            alarm.ExpirationTime = expirationTime;
            alarm.RecurrenceType = recurrence;
            ScheduledActionService.Add(alarm);
        }

        private DateTime GetDate(DatePicker dateSelected, TimePicker timeSelected)
        {
            DateTime date = (DateTime)dateSelected.Value;
            DateTime time = (DateTime)timeSelected.Value;
            DateTime beginTime = date + time.TimeOfDay;
            return beginTime;
        }

        private void CreateReminder(string name, DateTime beginTime, DateTime expirationTime, RecurrenceInterval recurrence, Uri navigationUri)
        {
            Reminder reminder = new Reminder(name);
            reminder.Title = titleTextBox.Text;
            reminder.Content = contentTextBox.Text;
            reminder.BeginTime = beginTime;
            reminder.ExpirationTime = expirationTime;
            reminder.RecurrenceType = recurrence;
            reminder.NavigationUri = navigationUri;
            ScheduledActionService.Add(reminder);
        }

        private Uri GetUrl()
        {
            string parametro1 = param1TextBox.Text;
            string parametro2 = param2TextBox.Text;
            string queryString = string.Empty;
            if (!string.IsNullOrEmpty(parametro1) && !string.IsNullOrEmpty(parametro2))
            {
                queryString = "?param1=" + parametro1 + "&param2=" + parametro2;
            }
            else if (string.IsNullOrEmpty(parametro1) || string.IsNullOrEmpty(parametro2))
            {
                queryString = (parametro1 != null) ? "?param1=" + parametro1 : "?param2=" + parametro2;
            }
            Uri navigationUri = new Uri("/ShowParams.xaml" + queryString, UriKind.Relative);
            return navigationUri;
        }

        private RecurrenceInterval GetRecurrenceInterval()
        {
            if (dailyRadioButton.IsChecked.Value)
            {
                return RecurrenceInterval.Daily;
            }
            if (weeklyRadioButton.IsChecked.Value)
            {
                return RecurrenceInterval.Weekly;
            }
            if (monthlyRadioButton.IsChecked.Value)
            {
                return RecurrenceInterval.Monthly;
            }
            if (endOfMonthRadioButton.IsChecked.Value)
            {
                return RecurrenceInterval.EndOfMonth;
            }
            if (yearlyRadioButton.IsChecked.Value)
            {
                return RecurrenceInterval.Yearly;
            }
            return RecurrenceInterval.None;
        }

        private void ValidateDate(DateTime minorDate, DateTime majorDate, string errorMessage)
        {
            if (minorDate < majorDate)
            {
                MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK);
            }
        }
    }   
}